package org.lansir.af.ui;

import org.lansir.af.R;
import org.lansir.af.model.CalendarFactory;
import org.lansir.af.model.DayModel;
import org.lansir.af.view.AbstractCalendarCellView;
import org.lansir.af.view.CalendarCellEventListener;
import org.lansir.af.view.CalendarView;
import org.lansir.af.view.CalendarViewBuilder;
import org.lansir.af.view.CalendarViewFlipperHolder;
import org.lansir.af.view.DefaultCalendarCellView;
import org.lansir.af.view.DefaultCalendarCellViewParams;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MonthFragment extends CommonFragment {
	private static final String TAG = "MonthFragment";

	private CalendarView mCalendarView;
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		CalendarFactory.clearCache();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LinearLayout viewRoot = (LinearLayout) inflater.inflate(R.layout.fragment_month, null);
		CalendarViewBuilder.setCalendarCellClass(DefaultCalendarCellView.class);
		CalendarViewBuilder.setDateOfWeekHeader(getActivity().getResources().getStringArray(R.array.calendarWeekName));
		CalendarViewBuilder.setDatabase(userdb);
		CalendarViewBuilder.setOnCalendarCellSelectedListener(new CalendarCellEventListener() {
			@Override
			public void onSelectionChanged(AbstractCalendarCellView view, boolean isSelected) {
				
				// _memo.setText(view.getDayModel() + " isSelected=" +
				// isSelected);
			}

			@Override
			public void onLongPress(AbstractCalendarCellView view) {

			}

			@Override
			public void onNextMonth(DayModel model) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onLastMonth(DayModel model) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onClick(AbstractCalendarCellView view) {
				DayModel dayModel = view.getDayModel();
				int year = dayModel.getParentMonthModel().getParentYearModel().getYear();
				int month = dayModel.getParentMonthModel().getMonth()-1;
				int day =  dayModel.getDayOfMonth();
				if(dayModel.getMark()){
					
					Intent intent = new Intent("org.lansir.af.intent.action.ACTION_MODE_VIEW_CALENDARDAY");
					intent.putExtra("Year", year);
					intent.putExtra("Month", month);
					intent.putExtra("Day", day);
					startActivityForResult(intent, 1);
				}else{
					Intent intent = new Intent("org.lansir.af.intent.action.ACTION_MODE_NEW_ACTIVITY");
					intent.putExtra("Year", year);
					intent.putExtra("Month", month);
					intent.putExtra("Day", day);
					startActivityForResult(intent, 0);
				}
			}
		});
		CalendarViewBuilder.setCalendarCellViewParam(new DefaultCalendarCellViewParams());
		
		CalendarViewFlipperHolder holder = CalendarViewBuilder.build(MonthFragment.this.getActivity(), getResources());
		mCalendarView = holder.getCalendarView();
		Log.v(TAG, "mCalendarView==null:" + (mCalendarView==null));
		viewRoot.addView(holder.getViewFlipper(), new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
		return viewRoot;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK){
			switch(requestCode ){
			case 0:
				mCalendarView.refreshCalendar();
				break;
			case 1:
				CalendarFactory.clearCache();
				mCalendarView.refreshCalendar();
				break;
			}
			
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	

}
